/**
 * User: 蕨菜
 * Date: 2019-05-15
 * Time: 11:48
 * Description: 此文件简要说明
 */

const Router = require('koa-router');
const querystring = require('querystring');

const studentRouter = new Router();

const { AppUtil } = require('../util');

const {
    addToModel,
    updateModelById,
    associationModel,
    getAll,
    getDataById,
    getDataByIdList,
    deleteById,
    deleteByIdWithAssociationModel,
} = require('../helper');

const validates = [{
    key: 'name',
    type: 'string',
    comment: '学生姓名'
}, {
    key: 'phone',
    type: ['string', 'number'],
    comment: '手机号'
}, {
    key: 'studentId',
    type: ['string', 'number'],
    comment: '学号'
}, {
    key: 'father',
    type: 'string',
    comment: '父亲姓名'
}, {
    key: 'fatherPhone',
    type: ['string', 'number'],
    comment: '父亲手机号'
}, {
    key: 'mother',
    type: 'string',
    comment: '母亲姓名'
}, {
    key: 'motherPhone',
    type: ['string', 'number'],
    comment: '母亲手机号'
}, {
    key: 'address',
    type: 'string',
    comment: '家庭住址'
}, {
    key: 'active',
    type: 'boolean',
    comment: '激活状态'
}];

const {
    Student,
    Class,
    Grade,
} = require('../models');

const {
    addStudent,
    deleteStudent,
    updateStudent,
    getStudent
} = require('../actions/student')

studentRouter.post('/updateStudent', async (ctx) => {

    const body = ctx.request.body || {};
    const { isValid, message } = AppUtil.validateParams(validates, body);

    if (!isValid) {
        ctx.body = message;
        return;
    }

    if (AppUtil.isEmpty(body.id)) {
        return await addStudent(body, ctx);
    }
    return await updateStudent(body, ctx);

})

studentRouter.post('/deleteStudent', async (ctx) => {
    const body = ctx.request.body || {};
    if (AppUtil.isEmpty(body.id)) {
        return ctx.body = AppUtil.requestFailed('id不能为空');
    }

    const data = await deleteById(Student, body.id);
    return ctx.body = {
        success: true,
        data
    }
})

studentRouter.get('/getStudent', async (ctx) => {

})

module.exports = studentRouter;
